package ysf.com.simplediffculty;


import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * @author ysf
 * @date 2021/6/2 22:52
 */
public class FirstTwoSum {

    /**
     * Given an array of integers, return indices of the two numbers such that they add up to a specific target
     *  给定一个整数数组，返回两个数字的索引，使它们相加为特定目标
     * You may assume that each input would have exactly one solution, and you may not use the same element twice
     *  您可以假设每个输入都只有一个解决方案，并且您不能两次使用相同的元素
     * @param args
     */
    public static void main(String[] args) {

       /*
       * Given nums = [2, 7, 11, 15], target = 9,
       *         Because nums[0] + nums[1] = 2 + 7 = 9,
       * return [0, 1]
       *
       * 在数组中找到2个数之和等于给定的值得数字,结果返回2个数字在数组中的下标。
       * 时间复杂度时O(n)
       */
        int[] nums = {9,8,11,15,2,7};
        System.out.println("nums of length=====>"+nums.length);
        int target = 9;
        int[] ints = towSumOne(nums, target);
        System.out.println("arrays ints is=======>"+ JSON.toJSONString(ints));

        int[] ints1 = twoSumTwo(nums, target);
        System.out.println("arrays ints1 is=======>"+ JSON.toJSONString(ints1));

        int[] ints2 = twoSumThree(nums, target);
        System.out.println("arrays ints2 is=======>"+ JSON.toJSONString(ints2));

    }

    /**
     * 解法一  两重循环
     * 时间复杂度O(n²)
     * @param nums
     * @param target
     * @return
     */
    public static int[] towSumOne(int[] nums,int target ){
        // 用于存放,数组中两个数字之和等于目标和的下标
        int [] ans = new int[2];
        for (int i = 0; i < nums.length; i++) {
            for (int j = (i+1); j <nums.length ; j++) {    // （i+1）保证两个循环遍历出的数字不是同一个数字
                if(nums[i] + nums[j] == target){
                        ans[0] = i;
                        ans[1] = j;
                        return ans;
                }
            }
        }
        return ans;
    }

    /**
     * 解法二
     * 时间复杂度为O(n)
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSumTwo(int[] nums,int target){

        Map<Integer,Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            // 将数组中数字根据下标一一对应放入Map集合中  数值作为key 下标为value
            map.put(nums[i],i);
        }
        for (int i=0;i<nums.length;i++){
            int sub = target - nums[i];
            if(map.containsKey(sub) && map.get(sub) != i){
                Integer integer = map.get(sub);
                return new int[]{i,map.get(sub)};
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    /**
     *  解法三 时间复杂度O(n)
     * @param nums
     * @param target
     * @return
     */
    public static int [] twoSumThree(int[] nums,int target){
        // 类比解法二少了一层for循环 此时map中并没有值
        Map<Integer,Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; i++) {
            int sub = target - nums[i];
            if(map.containsKey(sub)){
                return new int[]{i,map.get(sub)};
            }
            map.put(nums[i],i);
        }
        throw new IllegalArgumentException("No two sum solution");
    }



}
